package yqt.com.cnki.controller;
import cnki.tpi.util.ConstantUtil;
import haha.life.normal.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yqt.com.cnki.service.IQKYDService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author cb
 * @Description TODO
 * @date 2024/4/11
 */
@RestController
@RequestMapping("/file")
public class FileOperationController {

    @Autowired
    private IQKYDService qkydService;
    @RequestMapping(value="/showImg")
    public void ShowImg(String dbcode, String filename, HttpServletRequest request, HttpServletResponse response) throws IOException {
        BufferedInputStream buf =null;
        OutputStream outStream=response.getOutputStream(); //得到向客户端输出二进制数据的对象
        //获取缩略图信息
        String fileBasePath = qkydService.getQKYDFileBasePath(dbcode);
        String dbCode =dbcode.split("_")[0];
        String file = FileUtil.calculateAbstractPath(filename, fileBasePath + "\\" + dbCode + "\\Pages");
        //查询文件存储类型
            File files=new File(file);
            FileInputStream fileIs=null;
            byte data[];
            if (files.exists()){
                //存在文件则去本地取
                fileIs=new FileInputStream(file);
                //得到文件大小
                int i=fileIs.available();
                data=new byte[i];
                //读数据
                fileIs.read(data);
                outStream.write(data);  //输出数据
            }
            if (fileIs !=null) {
                fileIs.close();
            }

        response.setContentType("image/*"); //设置返回的文件类型
        outStream.flush();
        outStream.close();
        if (buf!=null){
            buf.close();
        }
    }
}
